package com.sk1.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.sk1.dao.UserRepository;
import com.sk1.model.User;
import com.sk1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.TypedQuery;
import java.util.List;

@Component
@Service
public class UserServiceImpl implements UserService {


    @Resource
    private UserRepository userRepository;

    @Resource
    private EntityManager entityManager;


    @Override
    public List<User> getAllUser() {
        TypedQuery<User> query = entityManager
                .createQuery("select u from User u", User.class)
                .setFirstResult(1)
                .setMaxResults(20);
        List<User> resultList = query.getResultList();

        //分页
        Pageable pageable = new PageRequest(1, 10);
        Page<User> all = userRepository.findAll(pageable);
        List<User> content = all.getContent();


        return resultList;
    }

    @Override
    public Object returnSomethings() {
        //分页
        Pageable pageable = new PageRequest(1, 10);
        Page<User> all = userRepository.findAll(pageable);
        List<User> content = all.getContent();
        return content;
    }
}
